# _*_ coding：UTF-8 _*_
# @Time：2021/9/416:40
# @Author：JYZ
# @Project：oneWork

import urllib.request

import random

url = "http://www.baidu.com/s?wd=ip"

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36'}

request = urllib.request.Request(url=url, headers=headers)

# 代理
# proxies = {"http": "118.24.219.151:16817"}

# 代理池
proxies_pool = {"http": "118.24.219.151:16817", "http": "118.24.219.151:16817", "http": "118.24.219.151:16817"}

proxies = random.choice(proxies_pool)

handle = urllib.request.ProxyHandler(proxies=proxies)

opener = urllib.request.build_opener(handle);

response = opener.open(request)

# response = urllib.request.urlopen(request)

content = response.read().decode("utf-8")

with open('agent.html', 'w', encoding="utf-8")as fp:
    fp.write(content)
